var notifyVm = new Vue({
    el: "#userPage",
    data: function () {
        return {
            condition: {
                notifyDateStart: '',
                notifyDateEnd: '',
                isEnable: '1'
            },
            pickerOptions: {
                shortcuts: [{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            },
            page: {
                current: 1,
                size: 10,
                total: 0,
                records: [],
                condition: {
                    expiryDateArray: '',
                    isEnable: '1'
                }
            },
            userDialogVisible: false,
            tableHeight: "",
            checkedList: []
        }
    },
    computed: {
        notifyDateArray: {
            get: function () {
                var arr = [];
                if (this.condition.notifyDateStart) {
                    var dateArr = this.condition.notifyDateStart.split("-");
                    var year = dateArr[0];
                    var month = dateArr[1];
                    var day = dateArr[2];
                    var date = new Date().setFullYear(year, month - 1, day);
                    arr[0] = date;
                }
                if (this.condition.notifyDateEnd) {
                    var dateArr = this.condition.notifyDateEnd.split("-");
                    var year = dateArr[0];
                    var month = dateArr[1];
                    var day = dateArr[2];
                    var date = new Date().setFullYear(year, month - 1, day);
                    arr[1] = date;
                }
                return arr;
            },
            set: function (newValue) {
                if (newValue.length > 0 && newValue[0]) {
                    this.condition.notifyDateStart = newValue[0].format('yyyy-MM-dd');
                }
                if (newValue.length > 1 && newValue[1]) {
                    this.condition.notifyDateEnd = newValue[1].format('yyyy-MM-dd');
                }
            }
        }
    },
    watch: {},
    methods: {
        search: function () {
            this.page.condition = this.condition;
            this.loadData();
        },
        loadData: function () {
            VanyAjax("/sys/notify/list", {
                current: this.page.current,
                size: this.page.size,
                condition: this.page.condition,
            }, "GET", ".el-table").done(function (data) {
                this.page.current = data.current;
                this.page.size = data.size;
                this.page.total = data.total;
                this.page.records = data.records;
                this.page.other = data.other;
                VanyAjax.prototype.hideLoading();
            }, false, this);
        },
        // 勾选角色的时候
        handleSelectionChange: function (selection) {
            this.checkedList = selection;
        },
        handleCurrentChange: function (val) {
            this.page.current = val;
            this.loadData();
        },
        handleSizeChange: function (val) {
            this.page.size = val;
            this.loadData();
        },
        add: function () {
            addNotifyVm.isEdit = false;
            addNotifyVm.notifyForm.id = "";
            addNotifyVm.show();
        },
        edit:function(row){
            addNotifyVm.isEdit = true;
            addNotifyVm.notifyForm.id = row.id;
            addNotifyVm.notifyForm.expiryDate = row.expiryDate;
            addNotifyVm.notifyForm.content = row.content;
            addNotifyVm.notifyForm.isEnable = row.isEnable;
            addNotifyVm.show();
        }
    },
    mounted: function () {
        this.$el.style.left = "210px";
        this.tableHeight = window.innerHeight - 400;
        this.search();
    },
    updated: function () {
        Jurisdiction();
    }
});


/*添加角色*/
var addNotifyVm = new Vue({
    el: "#addNotify",
    data: function () {
        return {
            notifyFormVisible: false,
            labelPosition: 'right',
            isEdit: false,
            notifyForm: {
                id: "",
                expiryDate: "",
                content: "",
                isEnable: "1"
            },
            rules: {
            }
        };
    },
    computed: {
        expiryDate: {
            get: function () {
                if (this.notifyForm.expiryDate) {
                    var dateArr = this.notifyForm.expiryDate.split("-");
                    var year = dateArr[0];
                    var month = dateArr[1];
                    var day = dateArr[2];
                    return new Date().setFullYear(year, month - 1, day);
                }
                return null;
            },
            set: function (newValue) {
                if (newValue) {
                    this.notifyForm.expiryDate = newValue.format('yyyy-MM-dd');
                } else {
                    this.notifyForm.expiryDate = '';
                }
            }
        }
    },
    methods: {
        submitForm: function (formName, e) {
            var self = this;
            this.$refs[formName].validate(function (valid) {
                if (valid) {
                    var url = self.isEdit ? "/sys/notify/modifyNotify" : "/sys/notify/addNotify";
                    VanyAjax(url, self.notifyForm).done(function (data) {
                        this.hide();
                        this.resetForm("notifyForm");
                        notifyVm.loadData();
                    }, true, self);
                } else {
                    VanyAjax.prototype.hideLoading();
                    e.stopPropagation();
                    return false;
                }
            });
        },
        show: function () {
            this.notifyFormVisible = true;
        },
        hide: function () {
            this.notifyFormVisible = false;
        },
        //重置
        resetForm: function (formName) {
            try {
                this.$refs[formName].resetFields();
            } catch (e) {

            }
            this.notifyForm.id = "";
            this.notifyForm.expiryDate = "";
            this.notifyForm.content = "";
            this.notifyForm.isEnable = "1";
        }
    },
    updated: function () {
        Jurisdiction();
    }
});